package io.renren.service.impl;

import io.renren.constant.AppConstant;
import io.renren.dao.CgsBusinessDao;
import io.renren.datasources.DataSourceNames;
import io.renren.datasources.annotation.DataSource;
import io.renren.entity.*;
import io.renren.service.CgsBusinessService;
import io.renren.vo.RespModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by ThinkPad on 2018/12/3.
 */
@Service
public class CgsBusinessServiceImpl implements CgsBusinessService {

    private Logger logger = LoggerFactory.getLogger(CgsBusinessService.class);

    @Autowired
    private CgsBusinessDao cgsBusinessDao;

    @Override
    public List<Map> queryReservations(String bookType) {
        return cgsBusinessDao.queryReservations(bookType);
    }

    @Override
    public List<Map> queryCgsInfoList(String type) {
        return cgsBusinessDao.queryCgsInfoList(type);
    }

    @Override
    public List<Map> queryUserReservations(CgsBookInfo cgsBookInfo) {
        return cgsBusinessDao.queryUserReservations(cgsBookInfo);
    }

    @Override
    public Integer saveBusinessBookInfo(CgsBookInfo cgsBookInfo) {
        return cgsBusinessDao.saveBusinessBookInfo(cgsBookInfo);
    }

    @Override
    public Integer cancelBookInfo(String bookId) {
        return cgsBusinessDao.cancelBookInfo(bookId);
    }

    public List<Map> queryBookTimeReservations(CgsBookInfo cgsBookInfo) {
        return cgsBusinessDao.queryBookTimeReservations(cgsBookInfo);
    }

    @Override
    public Integer updateBookInfoArrival(CgsBookInfo cgsBookInfo) {
        return cgsBusinessDao.updateBookInfoArrival(cgsBookInfo);
    }

    @Override
    public List<Map> queryBusinessDetail(Map paramMap) {
        return cgsBusinessDao.queryBusinessDetail(paramMap);
    }

    @Override
    public RespModel savePostInfo(CgsPolicePost cgsPolicePost) {

        if ("01".equals(cgsPolicePost.getPayType())) {
            cgsPolicePost.setStatus("2");//待付款
        } else {
            cgsPolicePost.setStatus("0");//货到付款
        }
        cgsPolicePost.setId(UUID.randomUUID().toString());
        cgsPolicePost.setOrderNum(String.valueOf(System.currentTimeMillis()));

        //工本费
        List<Map> nominalFee = cgsBusinessDao.queryPolicePostFee(new HashMap() {{
            put("feeType", "1");
            put("businessId", cgsPolicePost.getBusinessId());
        }});
//        邮寄费
        List<Map> postFee = cgsBusinessDao.queryPolicePostFee(new HashMap() {{
            put("feeType", "2");
            put("businessId", cgsPolicePost.getBusinessId());
        }});
        double postFee_value = postFee.stream()
                .filter(freeMap -> cgsPolicePost.getPostFeeId().equals(freeMap.get("id")))
                .mapToDouble(freeMap -> Double.parseDouble(((String) freeMap.get("feePrice"))))
                .sum();
        cgsPolicePost.setPostFee(String.valueOf(postFee_value));
        double nominalFee_value = nominalFee.stream()
                .mapToDouble((freeMap) -> Double.parseDouble(((String) freeMap.get("feePrice"))))
                .sum();
        cgsPolicePost.setMoninalFee(String.valueOf(nominalFee_value));
        cgsPolicePost.setTotalPrice(String.valueOf(postFee_value + nominalFee_value));
        Integer insert_count = cgsBusinessDao.insertPolicePostInfo(cgsPolicePost);
        if (insert_count == null || insert_count != 1) {
            return new RespModel().getRespModelInstance(AppConstant.STATU_FAIL, "警邮办保存失败", null);
        } else {
            return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", null);
        }
    }

    @Override
    public List<CgsPolicePost> queryMyPolicePostInfo(String userid) {
        return cgsBusinessDao.queryMyPolicePostInfo(userid);
    }

    @Override
    public Integer insertReconsiderInfo(CgsReconsider cgsReconsider) {
        return cgsBusinessDao.insertReconsiderInfo(cgsReconsider);
    }

    @Override
    public List<CgsReconsider> queryMyReconsiderList(String userid) {
        return cgsBusinessDao.queryMyReconsiderList(userid);
    }

    @Override
    public Integer insertPassportApply(CgsPassportApply cgsPassportApply) {
        return cgsBusinessDao.insertPassportApply(cgsPassportApply);
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public Integer getSequenceNumber() {
        return cgsBusinessDao.getSequenceNumber();
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public Integer insertPassportApplyToOracle(CgsPassportApply cgsPassportApply) {
        return cgsBusinessDao.insertPassportApplyOracle(cgsPassportApply);
    }

    @Override
    public List<CgsPassportApply> queryMyPassportApply(String userId) {
        return cgsBusinessDao.queryMyPassportApply(userId);
    }

    @Override
    public Integer insertCgsEprInfoBusCheck(CgsEprBusCheck cgsEprBusCheck) {
        return cgsBusinessDao.insertCgsEprInfoBusCheck(cgsEprBusCheck);
    }

    @Override
    public List<CgsEprBusCheck> queryMyCgsEprInfoBusCheckList(String userId) {
        return cgsBusinessDao.queryMyCgsEprInfoBusCheckList(userId);
    }

    @Override
    public List<Map> queryCgsBusinessTopicList(String businessType) {
        return cgsBusinessDao.queryCgsBusinessTopicList(businessType);
    }

    @Override
    public List<Map> queryCgsBusinessTopicDetail(String id) {
        return cgsBusinessDao.queryCgsBusinessTopicDetail(id);
    }

    @Override
    public List<Map> queryPassportTypeList() {
        return cgsBusinessDao.queryPassportTypeList();
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public List<CgsPassportApply> queryMyPassportApplyByOracle(List<String> xhList) {
        return cgsBusinessDao.queryMyPassportApplyByOracle(xhList);
    }

    @DataSource(name = DataSourceNames.FIRST)
    @Override
    public void updateStatus(CgsPassportApply cgsPassportApply) {
        cgsBusinessDao.updateStatus(cgsPassportApply);
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public List<Map<String, Object>> queryPassportTypeList1(String auditDept) {
        return cgsBusinessDao.queryPassportTypeList1(auditDept);
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public List<Map<String, Object>> queryPassportTypeList2(String passportId) {
        return cgsBusinessDao.queryPassportTypeList2(passportId);
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public List<RuleAreaEntity> queryRuleArea(String auditDept) {
        return cgsBusinessDao.queryRuleArea(auditDept);
    }

    @DataSource(name = DataSourceNames.THIRD)
    @Override
    public List<RuleLineEntity> queryRuleLine(String auditDept) {
        return cgsBusinessDao.queryRuleLine(auditDept);
    }

    @DataSource(name = DataSourceNames.FIRST)
    @Override
    public List<RoadMsgEntity> queryRoadMsgInfo(String auditDept){
        return cgsBusinessDao.queryRoadMsgInfo(auditDept);
    }

    @Override
    public List<Map> getDictValuesList(String type) {
        return cgsBusinessDao.getDictValuesList(type);
    }

    @Override
    public Integer insertAccidentCheck(CgsAccidentCheck cgsAccidentCheck) {
        return cgsBusinessDao.insertAccidentCheck(cgsAccidentCheck);
    }

    @Override
    public List<CgsAccidentCheck> queryAccidentCheckList(String userId) {
        return cgsBusinessDao.queryAccidentCheckList(userId);
    }

    @Override
    public List<Map> queryPolicePostFee(Map paramMap) {
        return cgsBusinessDao.queryPolicePostFee(paramMap);
    }

    /**
     * 主动方式更新订单支付状态成功
     *
     * @param orderNum
     * @return
     * @throws Exception
     */
    @Override
    public int setPostPaySuc(String orderNum, String wxOrderNum, String payTime) {
        return this.cgsBusinessDao.updatePostPay(orderNum, wxOrderNum, payTime, "0");
    }

    @Override
    public int setPostPayTk(String orderNum, String wxOrderNum, String payTime) {
        return this.cgsBusinessDao.updatePostPay(orderNum, wxOrderNum, payTime, "4");
    }

    @Override
    public int insertCgsQuestion(CgsQuestion cgsQuestion) {
        return cgsBusinessDao.insertCgsQuestion(cgsQuestion);
    }

    /**
     * 我的咨询列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Map> getMyQuestionList(String userId) {
        return this.cgsBusinessDao.queryMyCgsQuestionList(userId);
    }

    /**
     * 我的咨询列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map getHotQuestionList(int pageNo, int pageSize) {
        List<CgsQuestion> dataList = this.cgsBusinessDao.queryHotCgsQuestionList((pageNo - 1) * pageSize, pageSize);
        int totalCount = this.cgsBusinessDao.queryHotCgsQuestionCount();
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("dataList", dataList);
        resultMap.put("totalCount", totalCount);
        resultMap.put("totalPage", totalPage);
        return resultMap;
    }

    @Override
    public Integer insertCgsAppeal(CgsAppeal cgsAppeal) {
        return cgsBusinessDao.insertCgsAppeal(cgsAppeal);
    }

    /**
     * 我的诉求列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<CgsAppeal> getMyCgsAppealList(String userId) {
        return cgsBusinessDao.queryMyCgsAppealList(userId);
    }

    @Override
    public List<Map> queryCgsBusinessTopicDetailByBusinessId(String businessId) {
        return cgsBusinessDao.queryCgsBusinessTopicDetailByBusinessId(businessId);
    }
}
